package org.alanlau.hystrix.simple;

import com.netflix.hystrix.HystrixCommand;
import com.netflix.hystrix.HystrixCommandGroupKey;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

/**
 * 基本使用
 * @author liukun
 * @version 1.0
 * @date 2020/9/14 20:19
 */
public class MyHystrixCommand extends HystrixCommand<String> {
    private final String name;

    public MyHystrixCommand(String name) {
        super(HystrixCommandGroupKey.Factory.asKey("MyGroup"));
        this.name = name;
    }

    @Override
    protected String run() throws Exception {
        return this.name + ":" + Thread.currentThread().getName();
    }



    public static void main(String[] args) throws ExecutionException, InterruptedException {
        String result = new MyHystrixCommand("alan").execute();
        System.out.println("同步调用  " + result);

        Future<String> future = new MyHystrixCommand("alan").queue();
        System.out.println("异步调用  " + future.get());
    }
}
